import { Component, Injector, ViewChild } from '@angular/core';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { MessageService } from '@abp/message/message.service';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import {
    MakeTaskServiceProxy, ProductionAssignmentServiceProxy,
    OperationHistoryServiceProxy, ProductionDesignServiceProxy,
    MakeTaskInput, OperationLogDto, ProductionTaskOutput, CommonServiceProxy,
    StoreFunctionsServiceProxy
} from '@shared/service-proxies/service-proxies';
import { finalize, filter } from 'rxjs/operators';
import { AppComponentBase } from '@shared/common/app-component-base';


@Component({
    selector: 'app-rocket-mission',
    templateUrl: './rocket-mission.component.html',
    styleUrls: ['./rocket-mission.component.less']
})
export class RocketMissionComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    modelList: any = [];
    lotNoList: any = [];
    logList: OperationLogDto[];
    actived = false;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _makeTaskServiceProxy: MakeTaskServiceProxy,
        private _productionAssignmentServiceProxy: ProductionAssignmentServiceProxy,
        private _productionDesignServiceProxy: ProductionDesignServiceProxy,
        private _operationHistoryServiceProxy: OperationHistoryServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }
    tasklist: any = [];//任务编号列表
    queryInfo = {
        id: undefined,//id
        model: undefined,//规格型号
        lotNo: undefined,//发次
        jobNumber: undefined,//任务编号
    };//查询条件
    dialogType: string = '新增'
    addVisible: boolean = false;//弹窗 参数
    llVisible: boolean = false;//履历 参数
    addInfoUi: any = {
        id!: "",
        jobNumber!: "",//任务编号
        model!: "",//规格型号
        lotNo!: "",//发次
        drawingNo!: "",//产品图号
        productName!: "",//任务名称
        amount!: 0,//数量
        endDate!: undefined,//交付日期
        dispatcherId!: 0,//调度员id
        dispatcherName!: "",//调度员描述
        rockStuts: '',//火箭状态
        useWareHouse: "",//使用仓库
        remark!: "",//原因或依据 
    };//新增集合
    rockStutsOptions = [
        {
            label: '已开工',
            value: '已开工'
        }, {
            label: '总装完成',
            value: '总装完成'
        }, {
            label: '已发射',
            value: '已发射'
        }
    ];
    storeOptions = [];//仓库下拉
    userList: any[];//调度员下拉
    savingAddDia: boolean = false;//禁止频繁保存操作
    addInfo = new MakeTaskInput();
    selectedMembers: ProductionTaskOutput[];
    saveFlag = true;
    ngOnInit() {
        //仓库名称
        this._storeFunctionsServiceProxy.getStoreDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.storeOptions = [];
                this.storeOptions = result;
            });
        this.getmodelList()
        this.getUserList()
        this.getLotNoList()
    }
    add() {
        this.dialogType = '生产任务新增';
        this.addVisible = true;
        this.actived = true;
        this.addInfoUi.amount = 1;
        this.addInfoUi.rockStuts = { label: '已开工', value: '已开工' };
    }
    //重置
    reset() {
        this.queryInfo = {
            id: undefined,//id
            model: undefined,//规格型号
            lotNo: undefined,//发次
            jobNumber: undefined,//任务编号
        }
    }
    //弹窗隐藏
    displayclose(): void {
        this.actived = false;
        this.addInfoUi = {
            id!: "",
            jobNumber!: "",//任务编号
            model!: "",//规格型号
            lotNo!: "",//发次
            drawingNo!: "",//产品图号
            productName!: "",//任务名称
            amount!: 0,//数量
            endDate!: undefined,//交付日期
            dispatcherId!: 0,//调度员id
            dispatcherName!: "",//调度员描述
            rockStuts: '已开工',//火箭状态
            useWareHouse: "",//使用仓库
            remark!: "",//原因或依据 
        };//新增集合
    }
    // 新增
    addStandardDesc(): void {
        this.savingAddDia = true
        this.addInfo.amount = this.addInfoUi.amount
        this.addInfo.dispatcherId = this.addInfoUi.dispatcherId ? this.addInfoUi.dispatcherId.id : undefined
        this.addInfo.dispatcherName = this.addInfoUi.dispatcherId ? this.addInfoUi.dispatcherId.name : undefined
        this.addInfo.drawingNo = this.addInfoUi.drawingNo
        this.addInfo.endDate = this.addInfoUi.endDate
        this.addInfo.jobNumber = this.addInfoUi.jobNumber
        this.addInfo.modelId = this.addInfoUi.model.value
        this.addInfo.lotNoId = this.addInfoUi.lotNo.value
        this.addInfo.lotNo = this.addInfoUi.lotNo.label
        this.addInfo.model = this.addInfoUi.model.label
        this.addInfo.productName = this.addInfoUi.productName
        this.addInfo.rocketState = this.addInfoUi.rockStuts ? this.addInfoUi.rockStuts.value : undefined
        this.addInfo.storeId = this.addInfoUi.useWareHouse ? this.addInfoUi.useWareHouse.value : undefined
        this.addInfo.stroeName = this.addInfoUi.useWareHouse ? this.addInfoUi.useWareHouse.label : undefined
        this.addInfo.remark = this.addInfoUi.remark
        this.addInfo.id = this.addInfoUi.id
        if (this.saveFlag) {
            this.saveFlag = false;
            this._makeTaskServiceProxy.insertOrUpdateMakeTask(
                this.addInfo
            )
                .pipe(finalize(() => { this.savingAddDia = false }))
                .subscribe(result => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this.addVisible = false
                        this._messageService.success('添加成功');
                        this.getDescList()
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }
    }
    modelChangeEv(ev) {
        this.addInfoUi.jobNumber = this.addInfoUi.model.label + '/'
    }
    //型号下拉
    getmodelList() {
        this._commonServiceProxy.getMakeTaskModelList(
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.modelList = result
            });
    }
    /**
     * 发次
        @param lotstr — (optional)
        @param model — (optional)
     */
    getLotNoList(): any {
        this._commonServiceProxy.getLotMakeTaskNoList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.lotNoList = result
            });
    }
    /**
    * 调度员  
    */
    getUserList(): void {
        this._productionDesignServiceProxy.getDispatcherList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.userList = result
            });
    }
    /**
     * 生产任务
     * @param model — (optional)
       @param lotNo — (optional)
       @param jobNumber — (optional)
     */
    getDescList(event?: LazyLoadEvent): void {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this._makeTaskServiceProxy.getAssignments(
            this.queryInfo.id,
            this.queryInfo.model ? this.queryInfo.model.label : undefined,
            this.queryInfo.lotNo ? this.queryInfo.lotNo.label : undefined,
            this.queryInfo.model ? this.queryInfo.model.value : undefined,
            this.queryInfo.lotNo ? this.queryInfo.lotNo.value : undefined,
            this.queryInfo.jobNumber,
            this.primengTableHelper.getSorting(this.dataTable),//undefined
            this.primengTableHelper.getMaxResultCount(this.paginator, event),//10
            this.primengTableHelper.getSkipCount(this.paginator, event)//0
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();

            });
    }
    //履历
    llVisibleEv(id) {
        this._operationHistoryServiceProxy.getOperationLogList(
            id
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.logList = result
                this.llVisible = true
            });

    }
    //查看
    seeDetail(item) {
        this.actived = true;
        this.addInfoUi = JSON.parse(JSON.stringify(item))
        this.dialogType = '生产任务查看';
        this.addVisible = true;
        let modelArr = this.modelList.filter((ele, index) => {
            return ele.value == item.modelId
        })
        this.addInfoUi.model = modelArr[0]
        let lonoArr = this.lotNoList.filter((ele, index) => {
            return ele.value == item.lotNoId
        })
        this.addInfoUi.lotNo = lonoArr[0]
        this.addInfoUi.dispatcherId = { id: item.dispatcherId, name: item.dispatcherName };
        this.addInfoUi.rockStuts = { label: item.rocketState, value: item.rocketState };
        this.addInfoUi.useWareHouse = { label: item.stroeName, value: item.storeId };
        this.addInfoUi.endDate = item.endDate._d

    }
    //修改
    editDetail(item) {
        this.actived = true;
        this.addInfoUi = JSON.parse(JSON.stringify(item))
        this.dialogType = '生产任务修改';
        this.addVisible = true;
        let modelArr = this.modelList.filter((ele, index) => {
            return ele.value == item.modelId
        })
        this.addInfoUi.model = modelArr[0]
        let lonoArr = this.lotNoList.filter((ele, index) => {
            return ele.value == item.lotNoId
        })
        this.addInfoUi.lotNo = lonoArr[0]
        this.addInfoUi.dispatcherId = { id: item.dispatcherId, name: item.dispatcherName };
        this.addInfoUi.rockStuts = { label: item.rocketState, value: item.rocketState };
        this.addInfoUi.useWareHouse = { label: item.stroeName, value: item.storeId };
        this.addInfoUi.endDate = item.endDate._d
    }

    setSelectProps(val) {
        return {
            label: val,
            value: val
        }
    }


}